package com.hh.controller;

import com.hh.dao.PersonDao;
import com.hh.entity.Person;
import com.hh.service.PersonService;
import com.hh.service.imp.PersonServiceImp;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpRequest;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.UnsupportedEncodingException;
import java.util.List;

@Controller
@RequestMapping("/hh")
public class TestVontroller {
    @Autowired
    private PersonServiceImp personServiceImp;

    @GetMapping("/test")
    public ModelAndView test() {
        ModelAndView mv = new ModelAndView();
        mv.addObject("hello2", "这是第二个");
        mv.setViewName("/a.jsp");
        return mv;

    }

    @GetMapping("/updata")
    public void upadata(HttpServletRequest request, HttpServletResponse response) {
        try {
            request.setCharacterEncoding("UTF-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        Person person = new Person();
        person.setName(request.getParameter("name"));
        person.setAge(Integer.valueOf(request.getParameter("age")));
        personServiceImp.updata(person);

    }

    @GetMapping("/testPerson")
    @ResponseBody
    public void testPerson(Person person) {
        System.out.println(person);
    }

    @GetMapping("/testPerson2")
    @ResponseBody
    public Person testPerson2(Integer id) {
        Person person = personServiceImp.byId(id);
        return person;
    }

    @GetMapping("/testPerson3")
    @ResponseBody
    public List testPerson3() {
        return personServiceImp.findAll();
    }


    /*获取表单数据，要让input中的name和java中的参数保持一致就可以了
    * 注意spring mvc只会自动为以下五个参数进行自动赋值：
    * HttpServletRequest
HttpServletResponse
HttpSession
请求携带的参数
用于承载数据的Model*/
    @GetMapping("/getFrom")
    public void getFrom(String user, String passWord,HttpServletRequest request) {
        try {
            request.setCharacterEncoding("UTF-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        System.out.println(user + "逗我" + passWord);
    }

}
